<?php

namespace App\Http\Controllers\Mobile;

use App\Interfaces\CollectionService;
use App\Interfaces\GoodsService;
use App\Interfaces\ShopService;
use App\Interfaces\UserCollectionService;
use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class CollectionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,CollectionService $collectionService,GoodsService $goodsService,ShopService $shopService)
    {
        $user = Auth::user();
        if(!$user){
            return redirect('/user/login');
        }
        //dd($user);
        $type = $request->get('type');
        $page = $request->get('page') ?: Paginator::resolveCurrentPage('page');
        $paginate = $this->get($user->id,$type,$page,$collectionService,$goodsService,$shopService);
//dd($paginate);
        return view('mobile.collection.index',compact('paginate','type','user'));
    }

    public function getCollection(Request $request,CollectionService $collectionService,GoodsService $goodsService,ShopService $shopService){
        //$user_id =59;
        //$user = Auth::user();
        $user_id = $request->get('user_id');
        $type = $request->get('type');
        $page = $request->get('page');
        $paginate = $this->get($user_id,$type,$page,$collectionService,$goodsService,$shopService);
        $data = $paginate->toArray();
        return response()->json(['status'=>1,'data'=>$data]);

    }

    public function get($user_id,$type,$page,CollectionService $collectionService,GoodsService $goodsService,ShopService $shopService){
        $data = array();
        if($type=='GOODS'){
            $collection = $collectionService->getUserCollectionByType($user_id,$type)->toArray();
            $collectionGoods = $collection['data'];
            for($i=0,$j=0;$i<count($collectionGoods);$i++){
                if($goodsService->get($collectionGoods[$i]['reference_id'])){
                    $data[$j] = $goodsService->get($collectionGoods[$i]['reference_id'])->toArray();
                    $j++;
                }
            }

        }
        if($type=='SHOP'){
            $collection = $collectionService->getUserCollectionByType($user_id,$type)->toArray();
            $collectionShop = $collection['data'];
            for($i=0,$j=0;$i<count($collectionShop);$i++){
                if($shopService->get($collectionShop[$i]['reference_id'])){
                    $shops = $shopService->get($collectionShop[$i]['reference_id'])->toArray();
                    $countGoods = $goodsService->countGoodsByShop($shops['id']);
                    $data[$j] = $shops;
                    $data[$i]['goodsNum'] = $countGoods;
                    //$data[$i]['goods'] = $goodsService->getShopGoods($data[$i]['id'])->take(6)->toArray();
                    $j++;
                }
            }

        }

        $paginate =  new LengthAwarePaginator($data, count($data), 8, $page, [
            'path' => Paginator::resolveCurrentPath(),
            'pageName' => 'page',
        ]);

        return $paginate;
    }

    public function addCollection(Request $request,UserCollectionService $userCollectionService){
        $user_id = Auth::user()->id;
        //$user_id = $request->get('user_id');
        $type = $request->get('type');
        $id = $request->get('reference_id');
        if($user_id==''){
            return redirect('/user/login');
            //return response()->json(['message'=>'请先登录']);
        }
        if($type==''){
            return response()->json(['message'=>'缺少请求参数type']);
        }
        if($id==''){
            return response()->json(['message'=>'缺少请求参数id']);
        }
        $addCollection = $userCollectionService->add($user_id,$type,$id);
        if($addCollection){
            return response()->json(['id'=>$addCollection->id]);
        }else{
            return response()->json(['message'=>'已收藏']);
        }
    }

    public function cancelCollection(Request $request,UserCollectionService $userCollectionService){
        $user_id = Auth::user()->id;
        $type = $request->get('type');
        $id = $request->get('reference_id');
        if($user_id==''){
            return redirect('/user/login');
            //return response()->json(['status'=>0,'message'=>'请先登录']);
        }
        if($type==''){
            return response()->json(['message'=>'缺少请求参数type']);
        }
        if($id==''){
            return response()->json(['message'=>'缺少请求参数id']);
        }
        $cancelCollection = $userCollectionService->cancel($user_id,$type,$id);
        if($cancelCollection ){
            return response()->json(['id'=>$id]);
        }else{
            return response()->json(['message'=>'取消收藏失败']);

        }
    }


}
